import torch
import torchvision.datasets
from torch import nn

dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True)

inputs = torch.tensor([1, 2, 3], dtype=torch.float32)
labels = torch.tensor([1, 2, 5], dtype=torch.float32)

inputs = torch.reshape(inputs, (1, 1, 1, 3))
labels = torch.reshape(labels, (1, 1, 1, 3))
loss = nn.L1Loss()

result = loss(inputs, labels)
print(result)
loss = nn.L1Loss(reduction='sum')  # L1loss不取平均值，只求和
result = loss(inputs, labels)
print(result)

loss_mse = nn.MSELoss()
result = loss_mse(inputs, labels)
print(result)

inputs = torch.tensor([0.1, 0.2, 0.3])
inputs = torch.reshape(inputs, (1, 3))
labels = torch.tensor([1])

loss_ce = nn.CrossEntropyLoss()
result = loss_ce(inputs, labels)
result.backward()
print(result)
